package com.zhiwang.planet.ui.my.withdraw

import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.graphics.Bitmap
import android.text.Editable
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvp.p.BindPresenter
import com.zhiwang.planet.mvp.p.GraphCodePresenter
import com.zhiwang.planet.mvp.v.AccountBindView
import com.zhiwang.planet.mvp.v.GraphView
import com.zhiwang.planet.mvp.v.SmsCodeView
import com.zhiwang.planet.mvvm.model.entity.UserDao
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.LoginActivity
import com.zhiwang.planet.ui.TextWatcherAdapter
import com.zhiwang.planet.ui.loginv2.LoginV2Activity
import com.zhiwang.planet.util.Toaster
import kotlinx.android.synthetic.main.ali_bind_dialog_layout.*
import javax.inject.Inject

class AliBindDialog : GraphView, SmsCodeView, AccountBindView {

    override fun onAuthExpired() {

        LoginV2Activity.start(dialog.context)

    }

    override fun onAccountBind() {
        dialog.dismiss()
        successCallBack.onBindSuccess()
    }

    @Inject
    lateinit var graphPresenter: GraphCodePresenter

    @Inject
    lateinit var userDao: UserDao

    @Inject
    lateinit var bindPresenter: BindPresenter


    private constructor()


    companion object SOUL {

        lateinit var successCallBack: OnBindSuccessCallBack
        lateinit var dialog: Dialog

        lateinit var ctx: Context
        var seed = System.currentTimeMillis().toString()
        var phone: String = ""

        fun create(context: Context, cb: OnBindSuccessCallBack, phone: String): AliBindDialog {
            this.phone = phone
            successCallBack = cb
            ctx = context

            val dialogView = createDialogView(context)

            dialog = AlertDialog.Builder(context, R.style.NormalMiddleAlertDialogStyle)
                    .setView(dialogView)
                    .create()

            dialog.window.setBackgroundDrawableResource(R.drawable.round_dialog_bg)
            return AliBindDialog()
        }

        private fun createDialogView(context: Context): View {

            val layoutInflater = LayoutInflater.from(context)

            val rootView = layoutInflater.inflate(R.layout.ali_bind_dialog_layout, null)

            return rootView

        }

    }


    /**
     * 常规方法
     */

    fun getGraphCode() {
        seed = System.currentTimeMillis().toString()
        graphPresenter.getBindGraphCode(seed)
    }

    fun getSmsCode() {
        val account = dialog.aliAccount.text.toString().trim()
        val name = dialog.realName.text.toString().trim()
        val graphCode = dialog.graphCodeEditor.text.toString().trim()
        if (check(account, name, graphCode))
            bindPresenter.getBindSmsCode(phone, seed, dialog.graphCodeEditor.text.toString().trim())
    }

    override fun onError(t: Throwable) {
        Toaster.message(t.localizedMessage)
        getGraphCode()
    }

    override fun onGraphCodeLoad(b: Bitmap) {
        dialog.graphCodeBtn.setImageBitmap(b)
    }

    override fun onSmsSend() {
        Toaster.message(R.string.message_sms_send)
        dialog.captchaTextView.start()
    }

    fun confirmBindAliAccount() {
        //ali ,ali name ,smscode, graphcode
        val aliAccount = dialog.aliAccount.text.toString()
        val realName = dialog.realName.text.toString()
        val smsCode = dialog.smsCodeEditor.text.toString()
        val graphCode = dialog.graphCodeEditor.text.toString()

        bindPresenter.bindAliAccount(aliAccount, realName, graphCode, smsCode)
    }


    private fun check(account: String, name: String, graphCode: String): Boolean {
        when {
            TextUtils.isEmpty(account) -> {
                Toaster.message(dialog.context.getString(R.string.input_aliaccount_please))
                return false
            }
            TextUtils.isEmpty(name) -> {
                Toaster.message(dialog.context.getString(R.string.input_realname_please))
                return false

            }
//            TextUtils.isEmpty(graphCode) -> {
//                Toasty.message(dialog.context.getString(R.string.input_graphcode_please))
//                return false
//            }
        }

        return true
    }


    fun show() {
        val activity = ctx as BaseActivity
        DaggerUserComponent.builder()
                .appComponent(activity.getAppComponent())
                .accountModule(AccountModule(activity))
                .autoDisposeModule(AutoDisposeModule(activity))
                .build()
                .inject(this)
        dialog.show()

        getGraphCode()
        dialog.graphCodeBtn.setOnClickListener { getGraphCode() }
        dialog.captchaTextView.setOnClickListener { getSmsCode() }
        dialog.confirmBindBtn.setOnClickListener { confirmBindAliAccount() }
        dialog.closeBtn.setOnClickListener { dialog.dismiss() }
        observeTextChangeWithTargetButtonEnableStatus(
                arrayOf(
                        dialog.aliAccount,
                        dialog.realName,
                        dialog.smsCodeEditor),
                dialog.confirmBindBtn)

        graphPresenter.graphView = this
        bindPresenter.smsCodeView = this
        bindPresenter.bindAccountView = this
    }

    fun observeTextChangeWithTargetButtonEnableStatus(views: Array<EditText>, target: View) {

        val textWatcher = object : TextWatcherAdapter() {
            override fun afterTextChanged(s: Editable?) {
                super.afterTextChanged(s)
                views.forEach {
                    if (it.text.isEmpty()) {
                        target.isEnabled = false
                        return
                    }
                }
                target.isEnabled = true
            }
        }

        views.forEach {
            it.addTextChangedListener(textWatcher)
        }
    }

    interface OnBindSuccessCallBack {
        fun onBindSuccess()
    }
}